package com.tt.DAO;

import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.jasper.tagplugins.jstl.core.Out;

import com.tt.Connection.DB;
import com.tt.shpping.User;

import sun.security.pkcs11.Secmod.DbMode;

public class UserManager {
	public static void deleteuser(int id) {
		Connection conn = null;
		Statement stmt = null;
		String sql = "delete from ruser where id = " +id;
		conn = DB.getConnection();
		stmt = DB.getStmt(conn);
		try {
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DB.closeConnection(conn);
			DB.closeStmt(stmt);
		}
		
	}
	
	public static void update(User u) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "update ruser set  phone = ?, addr = ? where id = "+ u.getId();
		conn = DB.getConnection();
		pstmt = DB.getPstmt(conn, sql);
		try {
			pstmt.setString(1, u.getPhone());
			pstmt.setString(2, u.getAddr());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DB.closPstmt(pstmt);
			DB.closeConnection(conn);
		}
  	}
	public static User validate(String username, String password) throws UserNotFoundException, PasswordNotFoundException {
		Connection conn = null;
		ResultSet rs = null;
		User user = null;
		conn = DB.getConnection();
		String sql = "select * from ruser where username = '"+username+"'";
		rs = DB.getResultSet(conn, sql);
			try {
				if(!rs.next()) {
					throw new UserNotFoundException();
				} else if(!rs.getString(2).equals(password)){
					throw new PasswordNotFoundException();
				}else {
					user = new User();
					user.setId(rs.getInt(1));
					user.setUsername(rs.getString(2));
					user.setPassword(rs.getString(3));
					user.setPhone(rs.getString(4));
					user.setAddr(rs.getString(5));
					user.setRdate(rs.getTimestamp(6));
					
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			
			}finally {
				DB.closeRs(rs);
				DB.closeConnection(conn);
			}
		return user;
	}
	
	public static void save(User u) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		conn = DB.getConnection();
		String sql = "insert into ruser values (null, ?, ?, ?, ?, ?)";
		pstmt = DB.getPstmt(conn, sql);
		System.out.println(pstmt);
		try {
			pstmt.setString(1, u.getUsername());
			pstmt.setString(2, u.getPassword());
			pstmt.setString(3, u.getPhone());
			pstmt.setString(4, u.getAddr());
			pstmt.setTimestamp(5, u.getRdate());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DB.closPstmt(pstmt);
			DB.closeConnection(conn);
		}	
	}
	
	public static boolean checkName(String s) {
		System.out.println(s);
		Connection conn = null;
		ResultSet rs = null;
		String sql = "select * from ruser where username = '"+s+"'";
		System.out.println(sql);
		conn = DB.getConnection();
		rs = DB.getResultSet(conn, sql);
		try {
			if(rs.next()){
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	public static List<User> getUsers(){
		List<User> list = new ArrayList<User>();
		Connection  conn = null;
		ResultSet rs = null;
		conn = DB.getConnection();
		
		String sql = "select * from ruser";
		rs = DB.getResultSet(conn, sql);
		try {
			while(rs.next()) {
				User user = new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setPhone(rs.getString(4));
				user.setAddr(rs.getString(5));
				user.setRdate(rs.getTimestamp(6));
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DB.closeRs(rs);
			DB.closeConnection(conn);
		}
		
		return list;
	}
	
}
